﻿using System;
using System.Security.Authentication;
using System.Security.Claims;
using System.Threading.Tasks;
using K9Nano.LayIM.Models;
using K9Nano.LayIM.Razor.Areas.IM.Dto;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace K9Nano.LayIM.Razor.Areas.IM.Controllers
{
    [Area("IM")]
    [Authorize]
    public class ChatController : Controller
    {
        private readonly IJsonSerializer _jsonSerializer;
        private readonly IChatManager _chatManager;
        private readonly LayIMOptions _options;

        public ChatController(IJsonSerializer jsonSerializer, IChatManager chatManager, IOptions<LayIMOptions> options)
        {
            _jsonSerializer = jsonSerializer;
            _chatManager = chatManager;
            _options = options.Value;
            ViewData["Title"] = _options.Title;
        }

        private Guid GetUserId()
        {
            var uid = User.FindFirstValue(_options.UserIdentifierClaimType);
            if (Guid.TryParse(uid, out var gid))
            {
                return gid;
            }
            throw new AuthenticationException($"{_options.UserIdentifierClaimType} not found in Claims");
        }

        [HttpGet]
        public IActionResult Staff()
        {
            var config = new LayIMPCConfiguration
            {
                init = new LayIMConfiguration.ConfigUrl
                {
                    url = "/api/im/chat/init"
                },
                uploadImage = new LayIMConfiguration.ConfigUrl
                {
                    url = "/api/im/file/image"
                },
                isAudio = true,
                isfriend = true,
                isgroup = true,
                title = _options.Title
            };

            var dto = new ChatDto
            {
                ConfigJson = _jsonSerializer.Serialize(config)
            };

            return View(dto);
        }

        [HttpGet]
        public async Task<IActionResult> Customer()
        {
            var userId = User.FindFirstValue(_options.UserIdentifierClaimType);
            var app = User.FindFirstValue(_options.ChatAppCodeClaimType);

            var mine = await _chatManager.GetUserAsync(userId, HttpContext.RequestAborted);

            var config = new LayIMMobileConfiguration
            {
                init = new LayIMInitResponseData
                {
                    mine = mine
                },
                uploadImage = new LayIMConfiguration.ConfigUrl
                {
                    url = "/api/im/file/image"
                },
                isAudio = true,
                brief = true,
            };

            var room = await _chatManager.GetRoomAsync(app, userId, HttpContext.RequestAborted);

            var chat = new LayIMChat
            {
                id = room.Id,
                name = room.Title,
                type = Constants.Chat_Type_Group,
                avatar = _options.DefaultGroupAvatar
            };

            var dto = new ChatDto
            {
                ConfigJson = _jsonSerializer.Serialize(config),
                ChatJson = _jsonSerializer.Serialize(chat)
            };

            return View(dto);
        }
    }
}